package com.yanqu.road.server.cmd.player;

import com.yanqu.road.entity.enums.eSexType;
import com.yanqu.road.pb.server.ServerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.NameMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_GET_RANDOM_NAME, desc = "获取随机名字")
public class GetRandomNameCmd extends AbstractCommand {

    private int type;

    private int sex;

    private String name = "";

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ServerProto.GetRandomNameReqMsg reqMsg = ServerProto.GetRandomNameReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        sex = reqMsg.getSex();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        eSexType sexType = eSexType.parse(sex);
        if (sexType == null) {
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        name = NameMgr.getRandomName(sexType, player.getLanguage());
    }

    @Override
    public void sendData(GamePlayer player) {
        ServerProto.GetRandomNameRespMsg.Builder respMsg = ServerProto.GetRandomNameRespMsg.newBuilder();
        respMsg.setName(name);
        player.sendPacket(Protocol.U_GET_RANDOM_NAME, respMsg);
    }
}
